// package-star/pages/brand-distribute-setting-delivery/index.js
const app = getApp();
import { navBack, tipToast } from '../../../common/env';
import { formatPrice } from '../../../common/utils';

Page({
    data: {
        settingInfo: {},
        unifiedShippingFee: '',
    },
    onLoad(options) {
        app.event.addListener('sendDistributeSettingSupplyInfo', this._handleSendSettingInfo.bind(this));
    },
    onUnload() {
        app.event.removeListener('sendDistributeSettingSupplyInfo');
    },
    _handleSendSettingInfo(settingInfo) {
        const unifiedShippingFee = formatPrice(settingInfo.unifiedShippingFee, true);
        this.setData({
            settingInfo,
            unifiedShippingFee
        });
    },
    handleChangeShippingFee(e) {
        const price = formatPrice(e.detail);
        const unifiedShippingFee = formatPrice(price, true);
        this.setData({
            unifiedShippingFee,
            'settingInfo.unifiedShippingFee': price,
        });
    },
    handleSave() {
        const { settingInfo } = this.data;
        const { unifiedShippingFee } = settingInfo;
        if (unifiedShippingFee === undefined || unifiedShippingFee === null) {
            tipToast('请设置配送费');
            return;
        }
        app.event.trigger('getDistributeSettingSupplyInfo', settingInfo);
        navBack();
    },
})
